//
//  LBStatisticsManager.m
//  LBStatistics
//
//  Created by WuShiHai on 19/5/24.
//  Copyright (c) 2019 Lebbay. All rights reserved.
//

#import "LBStatisticsManager.h"
#import "LBStatisticsTaskManager.h"
#import "LBStatisticsModel.h"
#import "LBStatisticsTask.h"

@interface LBStatisticsManager ()

@end

@implementation LBStatisticsManager

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)init{
    self = [super init];
    if (self)
    {
        [self addNotificationObserver];
    }
    return self;
}

/** 注册通知 */
- (void)addNotificationObserver{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidReceiveMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
}

#pragma mark - Notification

- (NSArray *)loadStatisticsFromDB{
    return [[LBStatisticsModel sharedInstance] queryAllStatistics];
}

- (void)appDidBecomeActive:(NSNotification *)noti{
    [self performSelector:@selector(startHistoryTask) withObject:nil afterDelay:5];
}

- (void)appWillResignActive:(NSNotification *)noti{
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    [[LBStatisticsTaskManager manager] cancelAllTask];
}

- (void)appDidReceiveMemoryWarning:(NSNotification *)noti{
    [[LBStatisticsTaskManager manager] clearTaskForMemoryWarning];
}

- (void)startHistoryTask{
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        //不删除
//        //处理一天以前的
//        /* 
//             {
//             awake = 4;
//             }
//         */
//        NSDictionary *deleteDict = [[StatisticsModel sharedInstance] deleteLongLongAgoLogs];
//        if (deleteDict.allKeys.count > 0) {
//            //生成一条error记录
//
//            NSMutableDictionary *requestParams = [NSMutableDictionary dictionaryWithDictionary:deleteDict];
//            [requestParams setObject:@"appad" forKey:@"subtype"];
//            
//            NSString *appInfo = [DeviceUtil appInfo];
//            [requestParams setObject:appInfo forKey:@"appinfo"];
//            
//            NSString *url=[[Config shareConfig].statisticsAddress stringByAppendingFormat:@"wm.js?StatType=apperrorlog"];//vdapp
//            NSString *params =[requestParams dictionarySortGetStr];
//            
//            StatisticsTaskData *oneErrorTaskData = [StatisticsTaskManager generateOneTaskData:url params:params type:StatisticsTaskTypeDefault action:nil];
//            [[StatisticsTaskManager manager] addTaskData:oneErrorTaskData];
//        }
        
        
        //获取所有的
        NSArray *historyTasks = [self loadStatisticsFromDB];
        for (LBStatisticsTaskData *taskData in historyTasks){
            [[LBStatisticsTaskManager manager] addTaskData:taskData];
        }
  
    });
}
@end
